﻿using BLL;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Tools;
using 纺织贸易管理系统;

namespace 后整理管理系统.BLL
{
  public   class 流转单BLL
    {
        public static void 保存(Liuzhuancard liuzhuandan,List<liuzhuanmingxi > mingxis,List <StockTable > stocks,List<danjumingxitable > buliaomingxi, List<ImportAndExportOfRawMaterials> RawMaterials)
        {
            var houzhengli = liuzhuandan.Beizhu;
            var cardnum = liuzhuandan.CardNum;
            LiuzhuancardService.InsertLiuzhuancard(liuzhuandan);
            foreach (var mingxi in mingxis)
            {
                mingxi.CardNum = liuzhuandan.CardNum;
            }
            liuzhuanmingxiService.Insertliuzhuanmingxilst(mingxis);
           
            foreach (var m in buliaomingxi)
            {
                m.danhao = liuzhuandan.CardNum;
                // stockFun.Add(x => x.orderNum == m.OrderNum &&
                //x.CKMC == liuzhuandan.StockName && x.BH == m.Bianhao && x.GH == m.ganghao && x.PM == m.pingming && x.kuanhao == m.kuanhao
                //&& x.CustomName == m.CustomName && x.Huahao == m.Huahao && x.YS == m.yanse && x.ColorNum == m.ColorNum && x.RKDH == m.rkdh);
                // //mingxiFun.Add(x => x.Bianhao == m.Bianhao && x.pingming == m.pingming
                // //               && x.guige == m.guige && x.yanse == m.yanse && x.ganghao == m.ganghao && x.rkdh == m.rkdh && x.rkdh == x.danhao);
                // mingxiFun.Add(x => x.ID == m.ID);
                var mxs = Connect.dbcontext.Queryable<danjumingxitable>().Where(x =>x.danhao==m.rkdh &&x.ganghao==m.ganghao&&x.yanse==m.yanse&&x.pingming==m.pingming ).ToList();
                foreach (var mx in mxs)
                {
                    mx.houzhengli = houzhengli;
                    mx.offerid = cardnum;
                    mx.ProducedQuantity += m.chengpingmishu;
                    mx.ProducedQuantity = mx.ProducedQuantity > mx.chengpingmishu ? mx.chengpingmishu : mx.ProducedQuantity;
                    stocks = Connect.dbcontext.Queryable<StockTable>().Where(x => x.orderNum == m.OrderNum &&
              x.CKMC == liuzhuandan.StockName && x.BH == m.Bianhao && x.GH == m.ganghao && x.PM == m.pingming && x.kuanhao == m.kuanhao
              && x.CustomName == m.CustomName && x.Huahao == m.Huahao && x.YS == m.yanse && x.ColorNum == m.ColorNum && x.RKDH == m.rkdh&&x.LiuzhuanCard==string.Empty ).ToList();
                   if(stocks.Count==0)
                    {
                        stocks = Connect.dbcontext.Queryable<StockTable>().Where(x => x.orderNum == m.OrderNum &&
            x.CKMC == liuzhuandan.StockName && x.BH == m.Bianhao && x.GH == m.ganghao && x.PM == m.pingming && x.kuanhao == m.kuanhao
            && x.CustomName == m.CustomName && x.Huahao == m.Huahao && x.YS == m.yanse && x.ColorNum == m.ColorNum && x.RKDH == m.rkdh ).ToList();
                    }
                    foreach (var s in stocks)
                    {
                        s.LiuzhuanCard = cardnum;
                        s.KZ=liuzhuandan.Kezhong;
                        s.houzhengli = houzhengli;
                        s.ShippingMark = liuzhuandan.ShippingMark;
                        s.Unit = liuzhuandan.Unit;
                        s.MaShu = liuzhuandan.MaShu;
                        s.YS = s.YS +(string.IsNullOrEmpty(liuzhuandan.BaseFabricColor )?string.Empty:   "/" + liuzhuandan.BaseFabricColor);
                        var inventory = s.MS;
                        s.MS = m.chengpingmishu;
                        Connect.dbcontext.Insertable(s).ExecuteCommand();
                        s.MS = inventory - m.chengpingmishu;
                        if (s.MS == 0)
                        {
                            Connect.dbcontext.Deleteable(s).ExecuteCommand();
                        }
                        else
                        {
                            s.houzhengli = string.Empty;
                            s.LiuzhuanCard = string.Empty;
                            s.MS = inventory - m.chengpingmishu;
                            Connect.dbcontext.Updateable(s).ExecuteCommand();
                        }
                    }
                }
                Connect.dbcontext.Updateable<danjumingxitable>(mxs).ExecuteCommand();
            }
                Connect.dbcontext.Insertable(buliaomingxi).ExecuteCommand();                
                var raws = RawMaterials.Where(x => !string.IsNullOrEmpty(x.Num)).ToList();
                Connect.dbcontext.Insertable(raws).ExecuteCommand();
        }
        public static void 拆分保存(Liuzhuancard liuzhuandan, List<liuzhuanmingxi> mingxis, List<StockTable> stocks, List<danjumingxitable> buliaomingxi)
        {
            var houzhengli = liuzhuandan.Beizhu;
            var cardnum = liuzhuandan.CardNum;
            LiuzhuancardService.InsertLiuzhuancard(liuzhuandan);
            foreach (var mingxi in mingxis)
            {
                liuzhuanmingxiService.Insertliuzhuanmingxi(mingxi);
            }
            foreach (var m in buliaomingxi)
            { 
                m.danhao = liuzhuandan.CardNum;
                m.houzhengli = houzhengli;
                m.offerid = cardnum;
                Connect.dbcontext.Updateable(m).ExecuteCommand ();
                var s = StockTableService.GetOneStockTable(x => x.orderNum == m.OrderNum &&
             x.CKMC == liuzhuandan.StockName && x.BH == m.Bianhao && x.GH == m.ganghao && x.PM == m.pingming && x.kuanhao == m.kuanhao
             && x.CustomName == m.CustomName && x.Huahao == m.Huahao && x.YS == m.yanse && x.ColorNum == m.ColorNum && x.RKDH == m.rkdh);
                s.LiuzhuanCard = cardnum;
                s.houzhengli = houzhengli;
                StockTableService.UpdateStockTable(s);
            }
        }
        public  static void 退回库存(List<danjumingxitable> fabricdocdetail)
        {
            //var fabricdocdetail=Connect.dbcontext.Queryable<danjumingxitable >().Where (x=>x.danhao==danhao).ToList ();
            if (fabricdocdetail.Count > 0)
            {
                var danhao = fabricdocdetail[0].danhao;
                var processCard = Connect.dbcontext.Queryable<Liuzhuancard>().First(x => x.CardNum == danhao);
                if (processCard != null)
                {
                    foreach (var item in fabricdocdetail)
                    {
                        var Inbound = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == item.rkdh && x.pingming == item.pingming && x.ganghao == item.ganghao && x.yanse == item.yanse);
                        if (Inbound != null)
                        {
                            Inbound.ProducedQuantity -= item.chengpingmishu;
                            Inbound.ProducedQuantity = Inbound.ProducedQuantity < 0?0:Inbound.ProducedQuantity ;
                            Connect.dbcontext.Updateable(Inbound).ExecuteCommand();
                        }
                        var inventorycolor = item.yanse;
                        if (!string.IsNullOrEmpty(processCard.BaseFabricColor))
                        {
                            inventorycolor = item.yanse + (string.IsNullOrEmpty(processCard.BaseFabricColor) ? string.Empty : "/" + processCard.BaseFabricColor);
                        }
                        var inventory = Connect.dbcontext.Queryable<StockTable>().First(x => x.LiuzhuanCard == danhao && x.GH == item.ganghao && x.YS == inventorycolor && x.PM == item.pingming);

                        if (inventory != null)
                        {
                            Connect.dbcontext.Deleteable(inventory).ExecuteCommand();
                            var oldinventory = Connect.dbcontext.Queryable<StockTable>().First(x => x.RKDH == inventory.RKDH && x.GH == inventory.GH && x.YS == item.yanse && x.PM == item.pingming && x.LiuzhuanCard == string.Empty);
                            if (oldinventory != null)
                            {
                                oldinventory.MS += item.chengpingmishu;
                                Connect.dbcontext.Updateable(oldinventory).ExecuteCommand();
                            }
                            else
                            {
                                inventory.houzhengli = string.Empty;
                                inventory.LiuzhuanCard = string.Empty;
                                inventory.YS = item.yanse;
                                Connect.dbcontext.Insertable(inventory).ExecuteCommand();
                            }
                        }
                    }
                }
            }
        }
        public static void 修改(Liuzhuancard liuzhuandan, List<liuzhuanmingxi> mingxis, List<StockTable> stocks, List<danjumingxitable> buliaomingxi, List<ImportAndExportOfRawMaterials> RawMaterials)
        { 
            DeleteLogService.InsertDeleteLog(new DeleteLog() { date = DateTime.Now, Log = $"修改流转卡号{liuzhuandan.CardNum} 内容是" + JsonConvert.SerializeObject(LiuzhuancardService.GetOneLiuzhuancard(x => x.CardNum == liuzhuandan.CardNum)), useID = Tools.CurrentUser.user.YHBH });
            var oldbuliao=Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == liuzhuandan.CardNum).ToList();
            退回库存(oldbuliao);
            Connect.dbcontext.Deleteable(liuzhuandan).ExecuteCommand ();
            liuzhuanmingxiService.Deleteliuzhuanmingxi(x => x.CardNum == liuzhuandan.CardNum);
            Connect.dbcontext.Deleteable<danjumingxitable>().Where (x => x.danhao == liuzhuandan.CardNum).ExecuteCommand();
            //var mxs = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.offerid == liuzhuandan.CardNum ).ToList();
            //mxs.ForEach(x => x.houzhengli = string.Empty);
            //Connect.dbcontext.Updateable<danjumingxitable >(mxs).ExecuteCommand ();
            //danjumingxitableService.Updatedanjumingxitable("houzhengli=''", x => x.offerid == liuzhuandan.CardNum);
            库存BLL.合并布料(liuzhuandan.CardNum);
            var raws=Connect.dbcontext.Queryable<ImportAndExportOfRawMaterials>().Where (x=>x.DocNum ==liuzhuandan.CardNum ).ToList();        
            Connect.dbcontext.Deleteable(raws).ExecuteCommand();
            保存(liuzhuandan,mingxis,stocks,buliaomingxi,raws);
           
        }
       public static void 安排生产(string liuzhuanCardNum,string ArrangementDoc, List<liuzhuanmingxi> crafts)
        {
            库存BLL.原料预出库(Connect.dbcontext.Queryable<ImportAndExportOfRawMaterials>().Where(x => x.DocNum == liuzhuanCardNum).ToList());
            var processCard = Connect.dbcontext.Queryable<Liuzhuancard>().First(x => x.CardNum == liuzhuanCardNum);
            if(processCard!= null)
            {
                processCard.ArrangementDate = DateTime.Now.ToString ();
                processCard.IsArrangementProduced=true;
                processCard.ArrangementMan = CurrentUser.user.YHMC;
                processCard.ArrangementDoc = ArrangementDoc;
                Connect.dbcontext.Updateable(processCard).ExecuteCommand();
                if (Connect.dbcontext.Queryable<liuzhuanmingxi>().Where(x => x.CardNum == ArrangementDoc).Count() == 0)
                {
                    foreach (var item in crafts)
                    {
                        item.CardNum = ArrangementDoc;
                    }
                    Connect.dbcontext.Insertable(crafts).ExecuteCommand();
                }
            }
        }
        public static void 取消排产(string liuzhuanCardNum)
        {
            库存BLL.原料预入库(Connect.dbcontext.Queryable<ImportAndExportOfRawMaterials>().Where(x => x.DocNum == liuzhuanCardNum).ToList());
            var processCard = Connect.dbcontext.Queryable<Liuzhuancard>().First(x => x.CardNum == liuzhuanCardNum);
            if (processCard != null)
            {
                processCard.ArrangementDate = string.Empty ;
                processCard.IsArrangementProduced = false;
                Connect.dbcontext.Updateable(processCard).ExecuteCommand();
                Connect.dbcontext.Deleteable<liuzhuanmingxi>(x => x.CardNum == processCard.ArrangementDoc).ExecuteCommand();
            }
        }
        public static void 删除流转卡(string liuzhuankahao)
        {
            流转单BLL.退回库存(Connect.dbcontext.Queryable<danjumingxitable>().Where (x=>x.danhao==liuzhuankahao).ToList ());
            库存BLL.合并布料(liuzhuankahao);
            var liuzhuanka = LiuzhuancardService.GetOneLiuzhuancard(x => x.CardNum == liuzhuankahao);
            LiuzhuancardService.DeleteLiuzhuancard(x => x.CardNum == liuzhuankahao);
            liuzhuanmingxiService.Deleteliuzhuanmingxi(x => x.CardNum == liuzhuankahao);
            liuzhuanlogService.Deleteliuzhuanlog(x => x.CardNum == liuzhuankahao);
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == liuzhuankahao);
            Connect.dbcontext.Updateable<danjumingxitable>().SetColumns(x =>new danjumingxitable() { houzhengli = string.Empty,offerid=string.Empty }).Where(x => x.offerid == liuzhuankahao).ExecuteCommand();
            Connect.dbcontext.Updateable<StockTable>().SetColumns(x => new StockTable() { LiuzhuanCard = string.Empty , houzhengli = string.Empty }).Where(x => x.LiuzhuanCard == liuzhuankahao).ExecuteCommand();           
            DeleteLogService.InsertDeleteLog(new DeleteLog() { date = DateTime.Now, Log = $"删除流转卡号{liuzhuankahao} 内容是"+JsonConvert.SerializeObject (liuzhuanka), useID = Tools.CurrentUser.user.YHBH });
            var raws = Connect.dbcontext.Queryable<ImportAndExportOfRawMaterials>().Where(x => x.DocNum == liuzhuankahao).ToList();
            库存BLL.原料预入库(raws);
            Connect.dbcontext.Deleteable(raws).ExecuteCommand();
        }
    }
}
